
import scrollOpt from '../base/scrollOptimize'

cc.Class({
    extends: scrollOpt,

    properties: {
        mask: cc.Node,
        contentLayout: cc.Layout
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    update(dt) {
        if (this.canAni) {
            this.scrollTime += dt
            if (this.scrollTime >= this.timeToScroll) {
                this.timeToScroll = this.randomNum(2, 3)
                this.aniMation()
                this.scrollTime = 0
            }
        }
    },
    //数据，scale大小，是否自动滚
    init(grp, scale, ani) {
        this.scrollInit()
        this.scroll.scrollToTop()
        let sclseNum
        if (scale) {
            this.content.getComponent(cc.Layout).spacingX = 10 - 180 * (1 - scale)
            sclseNum = scale
        } else {
            this.content.getComponent(cc.Layout).spacingX = 10
            sclseNum = 1
        }

        this.scaleInit = sclseNum
        this.allNum = grp.length
        this.itemNum = 0
        for (let i = 0; i < grp.length; i++) {
            let pre = he.pool.getElement('adIconSec')
            pre.active = true
            pre.opacity = 255
            pre.position = cc.Vec2.ZERO
            // pre.y=-22
            pre.parent = this.content
            let spt = pre.getComponent('adIconSec')
            if ((i == 1 || i == 3 || i == 5) && ani) {
                spt.init(grp[i], true, sclseNum, this.callFun.bind(this))
            } else {
                spt.init(grp[i], -1, sclseNum, this.callFun.bind(this))
            }
        }
        this.contentLayout.updateLayout()
        this.canAni = true
        this.timeToScroll = this.randomNum(2, 3)
        this.mask.active = true
        this.scrollTime = 0
        this.scrollIndex = 0
        this.content.getComponent(cc.Layout).updateLayout()
        this.maxScrollOffset = this.scroll.getMaxScrollOffset();
    },

    callFun() {
        this.itemNum += 1
        if (this.itemNum >= this.allNum) {
            console.log('遮罩解除')
            this.mask.active = false
        }
    },


    localConvertWorldPointAR(node) {
        if (node) {
            return node.convertToWorldSpaceAR(cc.v2(0, 0));
        }
        return null;
    },


    aniMation() {
        if (this.scrollIndex < 2) {
            this.scroll.scrollToOffset(cc.v2(this.maxScrollOffset.x / 6 * (this.scrollIndex + 1), 0), 0.2)
        }
        else if (this.scrollIndex >= 2 && this.scrollIndex < 4) {
            this.scroll.scrollToOffset(cc.v2(this.maxScrollOffset.x / 6 * (4 - this.scrollIndex - 1), 0), 0.2)
        }
        for (let i = 0; i < this.content.children.length; i++) {
            let xPos = this.localConvertWorldPointAR(this.content.children[i]).x
            if (xPos < -90 || xPos > 840) {
                this.content.children[i].active = false
            } else {
                this.content.children[i].active = true
            }
        }
        this.scrollIndex++
        if (this.scrollIndex == 4) {
            this.scrollIndex = 0
        }
    },
    //范围内的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    },

    clearItem() {
        for (let i = (this.content.children.length); i > 0; i--) {
            this.content.children[i - 1].getComponent('adIconSec').stopSchedule()
            he.pool.putElement('adIconSec', this.content.children[i - 1])
        }
        this.closeListen()
        this.canAni = false
    },


});
